<?php
namespace app\index\controller;

use think\Request;

class Article extends Base
{


    public function index(Request $request)
    {
        $currCategoryId = $this->request->get('category_id',0);

        $article = new \app\common\models\Article();
        $where = ['status'=>1];
        if ($request->get('category_id',0) > 0){
            $where['category_id'] = $request->get('category_id');
        }

        $list = $article->getPageData($where)->toArray();

        $len = 200;
        $ext = '';
        foreach ($list['data'] as &$v){
            $content = strip_tags($v['content']);
            if (strlen($content) > $len){
                $ext = '...';
            }
            $v['content'] = mb_substr($content, 0, $len, 'utf-8').$ext;
            $v['create_time'] = date('Y-m-d',strtotime($v['create_time']));
        }

        //获取分类
        $articleCategory = new \app\common\models\ArticleCategory();

        $cateList = $articleCategory->getMany(['status'=>1,'pid'=>0])->toArray();


        return view('',['list'=>$list,'categoryList'=>$cateList,'currCategoryId'=>$currCategoryId]);
    }

    public function detail($id)
    {
        $currCategoryId = $this->request->get('category_id',0);

        $article = new \app\common\models\Article();
        $info = $article->getDataById($id)->toArray();

        //获取分类
        $articleCategory = new \app\common\models\ArticleCategory();

        $cateList = $articleCategory->getMany(['status'=>1,'pid'=>0])->toArray();

        //上一条
        $prevInfo = $article->getOne(['status'=>1, 'id'=>['<',$id]], 'id desc');

        //下一条
        $nextInfo = $article->getOne(['status'=>1, 'id'=>['>',$id]], 'id asc');

        return view('',['info'=>$info,'categoryList'=>$cateList,'prevInfo'=>$prevInfo,'nextInfo'=>$nextInfo,'currCategoryId'=>$currCategoryId]);
    }

}
